1 /**
2  * Authors: Harrison Ford, harrison@0xcc.pw
3  * Date: July 21, 2020
4  */
5 
6 module firecracker_d.models.machine_configuration;
7 import firecracker_d.models.cpu_template;
8 import asdf;
9 import firecracker_d.models.base_model;
10 
11 /***
12 * Describes the number of vCPUs, memory size, Hyperthreading capabilities and the CPU template.
13 ***/
14 struct MachineConfiguration {
15 	mixin BaseModel;
16 
17 	/***
18 	* CPU Template to use for default options
19 	***/
20 	@serializationKeys("cpu_template") CPUTemplate cpuTemplate;
21 
22 	/***
23 	* Option to enable hyperthreading for the guest
24 	***/
25     @serializationRequired 
26 	@serializationKeys("ht_enabled") bool htEnabled;
27 
28 	/***
29 	* Guest's memory size in MiB
30 	***/
31     @serializationRequired
32 	@serializationKeys("mem_size_mib") long memSizeMib;
33 
34     /***
35     * Enable dirty page tracking
36     ***/
37     @serializationRequired
38     @serializationKeys("track_dirty_pages") bool trackDirtyPages;
39 
40 	/***
41 	* Amount of vCPUs given to the guest
42 	***/
43     @serializationRequired 
44 	@serializationKeys("vcpu_count") long vcpuCount = 1;
45 
46 
47 	/***
48 	* Modify the microVM's configuration via the Firecracker API. 
49     * Throws: FirecrackerException on error.
50 	***/
51 	bool put(FirecrackerAPIClient cl) {
52 		Response r = cl.put("/machine-config", this.stringify);
53 
54 		if(r.code == 204) {
55 			return true;
56 		}
57 		else {
58 			throwFromResponse(r);
59 			return false;
60 		}
61 	}
62 
63     /***
64     * Partially updates the Machine Configuration of the VM. Pre-boot only. 
65     * Throws: FirecrackerException on error.
66     ***/
67 	bool patch(FirecrackerAPIClient cl) {
68 		Response r = cl.patch("/machine-config", this.stringify);
69 		if(r.code == 204) {
70 			return true;
71 		}
72 		else {
73 			throwFromResponse(r);
74 			return false;
75 		}
76 	}
77 
78 	/***
79 	* Get the microVM's config via the Firecracker API. 
80 	* Throws: FirecrackerException on error.
81 	***/
82 	this(FirecrackerAPIClient cl) {
83 		Response r = cl.get("/machine-config");
84 		if(r.code == 200) {
85 			MachineConfiguration m = r.responseBody.toString.deserialize!MachineConfiguration();
86 			this = m;
87 		}
88 		else {
89 			throwFromResponse(r);
90 		}
91 	}
92 
93     ///
94     invariant {
95         assert(vcpuCount < 32);
96         assert(vcpuCount > 0);
97     }
98 }